package com.rmy;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.rmy.common.core.text.Convert;
import com.rmy.domain.CompArea;
import com.rmy.domain.CompParam;
import com.rmy.service.impl.OnbonBx06CompDevice;
import com.rmy.service.impl.OnbonCompDevice;

import java.util.ArrayList;
import java.util.List;

public class CompUtils {


    /**
     * 通过参数获取驱动类型 1 仰邦
     * @param strParams
     * @return
     */
    public String getDriverType(String strParams) {
        JSONObject jsonObject = JSONObject.parse(strParams);
        return Convert.toStr(jsonObject.get("driver_type"));
    }

    /**
     * 解析成对象
     * @param strContent
     * @return
     */
    public static List<CompArea> parseContent(String strContent){
        List<CompArea> compAreaDynamicList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parse(strContent);
        JSONArray jsonArray = jsonObject.getJSONArray("areas");
        for (int i = 0;  i < jsonArray.size(); i++) {
            CompArea compAreaDynamic = JSONObject.parseObject(jsonArray.getJSONObject(i).toString(), CompArea.class);
            compAreaDynamicList.add(compAreaDynamic);
        }
        return compAreaDynamicList;
    }

    /**
     * 解析成对象
     * @param strParams
     * @return
     */
    public static CompParam parseParams(String strParams){
        CompParam compParam = JSONObject.parseObject(strParams, CompParam.class);
        return compParam;
    }

    public static boolean sendComp(String strIp, int nPort, String strParams, List<CompArea> compAreaList) throws Exception {
        CompParam compParam = parseParams(strParams);
        // 1 仰邦BX06 默认
        switch (Convert.toStr(compParam.driver_type, "1"))
        {
            case "2":
                new OnbonCompDevice().sendComp(strIp, nPort, compParam, compAreaList);
                break;
            default:
                new OnbonBx06CompDevice().sendComp(strIp, nPort, compParam, compAreaList);
                break;
        }
        return true;
    }

}
